﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Xml.Linq;

namespace SharePointWiki
{
    public partial class Preferences : Form
    {
        string keyValue = "Software\\GruberLogistics\\SharePointWiki\\";
        Microsoft.Win32.RegistryKey key;
        ThisAddIn addIn;

        public Preferences(ThisAddIn _addIn)
        {
            InitializeComponent();
            addIn = _addIn;
            initReg();
            load();
        }

        private void btn_save_Click(object sender, EventArgs e)
        {
            save();
            this.Close();
        }

        private void save()
        {
            key = Microsoft.Win32.Registry.CurrentUser.CreateSubKey(keyValue);
            key.SetValue("site", txt_site.Text);
            key.SetValue("wikiImageRootFolder", txt_wikiImageRootFolder.Text);
            key.SetValue("wikiImagesList", cmb_wikiImageList.Text);
            key.SetValue("wikilist", txt_wikilist.Text);
            key.SetValue("site_username", txt_username.Text);
            key.SetValue("site_password", txt_password.Text);
            key.SetValue("site_domain", txt_domain.Text);
            key.Close();
        }

        private void load()
        {
            key = Microsoft.Win32.Registry.CurrentUser.CreateSubKey(keyValue);
            txt_site.Text = key.GetValue("site").ToString();
            txt_wikiImageRootFolder.Text = key.GetValue("wikiImageRootFolder").ToString();
            txt_wikilist.Text = key.GetValue("wikilist").ToString();
            txt_username.Text = key.GetValue("site_username").ToString();
            txt_password.Text = key.GetValue("site_password").ToString();
            txt_domain.Text = key.GetValue("site_domain").ToString();
            cmb_wikiImageList.Text = key.GetValue("wikiImagesList").ToString();
            key.Close();
        }
        private void initReg()
        {
            key = Microsoft.Win32.Registry.CurrentUser.CreateSubKey(keyValue);
            if (key.GetValue("site_username") == null)
                key.SetValue("site_username", Environment.UserName);
            if (key.GetValue("site_domain") == null)
                key.SetValue("site_domain", Environment.UserDomainName);
            if (key.GetValue("site_password") == null)
                key.SetValue("site_password", "");
            if (key.GetValue("wikiImageRootFolder") == null)
                key.SetValue("wikiImageRootFolder", "");
            if (key.GetValue("wikiImagesList") == null)
                key.SetValue("wikiImagesList", "");
            if (key.GetValue("wikilist") == null)
                key.SetValue("wikilist", "");
            if (1 == 1) // for Gruber Logistics Settings
            {
                if (key.GetValue("site") == null)
                    key.SetValue("site", "http://intranet");
            }
            else
            {
                if (key.GetValue("site") == null)
                    key.SetValue("site", "");
            }
            key.Close();
        }

        private void btn_searchLists_Click(object sender, EventArgs e)
        {
            List<string> WikiIDs = addIn.getSpLists("119");
            txt_wikilist.Text = "";
            foreach (string id in WikiIDs)
                txt_wikilist.Text += id + Environment.NewLine;
        }

        
        private void btn_loadWikiImages_Click(object sender, EventArgs e)
        {
            loadWikiImageLists();
        }

        private void loadWikiImageLists()
        {
            loadComboboxWithNames(cmb_wikiImageList, addIn.getSpLists("109"));
        }

        public List<string> getWikiLists()
        {
            return txt_wikilist.Text.Split(Environment.NewLine.ToCharArray(), StringSplitOptions.RemoveEmptyEntries).ToList<string>();
        }

        public static void loadComboboxWithNames(ComboBox cb, List<string> ListIds)
        {
            cb.Items.Clear();
            foreach (string id in ListIds)
                cb.Items.Add(GetNameOfListId(id));
        }

        public static string GetNameOfListId(string id)
        {
            return id.Substring(id.IndexOf('(') + 1, id.LastIndexOf(')') - id.IndexOf('(') - 1);
        }

        public static string GetIdOfListId(string id)
        {
            return id.Substring(0, id.IndexOf('(') - 1);
        }

        public static string GetIdOfListId(List<string> ids, string name)
        {
            foreach (string id in ids)
                if (GetNameOfListId(id) == name)
                    return GetIdOfListId(id);
            return null;
        }

        private void Preferences_Load(object sender, EventArgs e)
        {
            loadWikiImageLists();
        }
    }
}

